import os
import sys
from openai import OpenAI

# 添加项目根目录到Python路径
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(current_dir))
sys.path.append(project_root)

from config_reader import get_bailian_config

# 从配置文件获取百炼配置
bailian_config = get_bailian_config()
bailian_api_token = bailian_config['token']

# 初始化阿里云百炼服务（兼容OpenAI接口）。
client = OpenAI(
    api_key=bailian_api_token,
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)


# 调用阿里云百炼嵌入 API
def get_embedding(text, model="text-embedding-v4"):
    response = client.embeddings.create(
        input=text,
        model=model,
        dimensions=1024,  # 指定向量维度
        encoding_format="float"
    )
    return response.data[0].embedding

# 示例文本
text = "Hello, world!"

# 获取嵌入向量
embedding = get_embedding(text)

print("Embedding vector:", embedding)
